<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 搜索接口
 * @ApiWeigh (84)
 */
class Search extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 搜索历史
     * 
     * @ApiTitle    (搜索历史)
     * @ApiSummary  (获取用户搜索历史记录)
     * @ApiMethod   (GET)
     * @ApiParams   (name="type", type="string", required=true, description="搜索类型:goods=商品,video=视频,travel=文旅,service=服务,service_order=服务订单,goods_order=商品订单,travel_order=文旅订单,myvideo=我的课程,follow=关注")
     * @ApiReturn   ({
        "code": 1,
        "msg": "success",
        "data": [{
          "keyword": "搜索关键词",
          "createtime": "2024-01-01 00:00:00"
        }]
      })
     */
    public function history()
    {
        $type = $this->request->get('type');
        if (!in_array($type, ['goods', 'video', 'travel', 'service','service_order','goods_order','travel_order','myvideo','follow','teacher_service_order'])) {
            $this->error('不支持的搜索类型');
        }

        $list = \app\common\model\SearchHistory::where([
                'user_id' => $this->auth->id,
                'type' => $type
            ])
            ->field('keyword,createtime')
            ->order('createtime DESC')
            ->limit(20)
            ->select();

        $this->success('', $list);
    }

    /**
     * 清空搜索历史
     * 
     * @ApiTitle    (清空搜索历史)
     * @ApiSummary  (清空指定类型的搜索历史记录)
     * @ApiMethod   (POST)
     * @ApiParams   (name="type", type="string", required=true, description="搜索类型:goods=商品,video=视频,travel=文旅,service=服务,service_order=服务订单,goods_order=商品订单,travel_order=文旅订单,myvideo=我的课程,follow=关注")
     *  @ApiReturn   ({
            "code": 1,
            "msg": "清除成功",
            "data": null
        })
     */
    public function clearHistory()
    {
        $type = $this->request->post('type');
        if (!in_array($type, ['goods', 'video', 'travel', 'service','service_order','goods_order','travel_order','myvideo','follow','teacher_service_order'])) {
            $this->error('不支持的搜索类型');
        }

        \app\common\model\SearchHistory::where([
            'user_id' => $this->auth->id,
            'type' => $type
        ])->delete();

        $this->success('清除成功');
    }
}